package com.work.customer.controller;

import com.github.pagehelper.Page;
import com.work.customer.pojo.Role;
import com.work.customer.service.RoleService;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    //对用户的角色进行添加
    @PostMapping("/add")
    public Result add(@RequestParam("id") Integer uid,Integer[] rids) {
        try {
            roleService.add(uid,rids);
            return new Result(true, StatusCode.ADD_OK,"添加成功");
        }catch (Exception e){
            return new Result(true, StatusCode.ADD_ERROR,"添加失败");
        }

    }

    //对用户的角色进行修改
    @PutMapping("/update")
    public Result update(@RequestParam("id") Integer uid,Integer[] rids) {
        try {
            roleService.update(uid,rids);
            return new Result(true, StatusCode.UPDATE_Ok,"修改成功");
        }catch (Exception e){
            return new Result(true, StatusCode.UPDATE_ERROR,"修改失败");
        }

    }

    //对用户的角色进行删除
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("id") Integer uid,Integer[] rids) {
        try {
            roleService.delete(uid,rids);
            return new Result(true, StatusCode.DELETE_OK,"删除成功");
        }catch (Exception e){
            return new Result(true, StatusCode.DELETE_ERROR,"删除失败");
        }

    }

    //多条件查询+分页查询
    @GetMapping(value = "/find")
    public Result findPage(@RequestParam(required = false) Map searchMap,
                           @RequestParam(name = "page",defaultValue = "1",required = false) int page,
                           @RequestParam(name = "size",defaultValue = "5",required = false) int size
                           ){
        try {
            Page<Role> page1= roleService.findPage(searchMap, page, size);
            return new Result(true, StatusCode.FIND_Ok, "查询成功", page1);
        }catch (Exception e){
            return new Result(true, StatusCode.FIND_ERROR, "查询失败");
        }

    }

    //根据用户id查询用户角色
    @GetMapping(value = "/findById")
    public Result findById(@RequestParam("id") Integer uid){
        try {
            List<Role> list = roleService.findById(uid);
            return new Result(true, StatusCode.FIND_Ok, "查询成功", list);
        }catch (Exception e){
            return new Result(true, StatusCode.FIND_ERROR, "查询失败");
        }

    }

}
